<?php
/**
 * 加载sdk包以及错误代码包
 */
require_once 'sdk.class.php';

/**
 *
 */
class oss_client {
    /**
     * 上传文件最大大小
     * @var int
     */
    private $maxsize = 2097152;

    /**
     * @var string
     */
    private $serpath;
    /**
     * 上传文件扩展名
     * @var string
     */
    private $extension = "jpg,png,gif,bmp,jpeg";

    /**
     * @var string
     */
    public $bucket = "php-images";

    /**
     * 自动获取上传文件的信息，目前只能处理单个文件
     * @return array
     */
    private function fileinfo(){
        $fileinfo = array();
        foreach($_FILES as $v) {
            $fileinfo = $v;
            break;
        }
        $filename = $fileinfo['name'];
        return array_merge($fileinfo,pathinfo($filename));
    }

    /**
     * 自动重命名上传的临时文件
     * @return mixed|string
     */
    private function re_tmp_name(){
        $fileinfo = $this->fileinfo();
        //$ccg_name = str_replace(array('{','}','-'),'',com_create_guid());
        $ccg_name = $this->create_sn();
        $nt_path = str_replace(basename($fileinfo['tmp_name']),$ccg_name.".{$fileinfo['extension']}",$fileinfo['tmp_name']);
        if(rename($fileinfo['tmp_name'],$nt_path)) {
            return $nt_path;
        } else {
            return '';
        }
    }

    /**
     *
     */
    function __construct(){
        $this->oss_sdk_service = new ALIOSS();
        //设置是否打开curl调试模式
        $this->oss_sdk_service->set_debug_mode(FALSE);
        $this->serpath = date("Ym")."/".date("md")."/";
    }

    /**
     * 自动识别上传接口
     * 二进制文件流传送
     * @return string
     */
    public function oss_upload($rename=1){
        $fileinfo = $this->fileinfo();
        if(!in_array(strtolower($fileinfo['extension']),explode(",",$this->extension))) {
            return " {$fileinfo['extension']} extension is error!";
        }
        if($fileinfo['size']>$this->maxsize) return " {$fileinfo['size']} over max !";
        $re_tmp_name = $this->re_tmp_name();
        $oss_name = $rename?basename($re_tmp_name):$fileinfo['name'];
        $result = (array)$this->oss_sdk_service->create_mpu_object($this->bucket, $this->serpath.$oss_name,array(
            ALIOSS::OSS_FILE_UPLOAD => $re_tmp_name,
            'partSize' => $fileinfo['size'],
        ));
        @unlink($re_tmp_name);
        return $result['status']==200?$this->serpath.$oss_name:"";
    }

    /**
     * @return string
     * 作为文本传送
     */
    public function oss_upload_by_content(){
        $fileinfo = $this->fileinfo();
        $content = file_get_contents($fileinfo['tmp_name']);
        $upload_file_options = array('content' => $content, 'length' => strlen($content));
        $oss_name = $this->create_sn().".".$fileinfo['extension'];
        $result = (array)$this->oss_sdk_service->upload_file_by_content($this->bucket,$this->serpath.$oss_name, $upload_file_options);
        @unlink($fileinfo['tmp_name']);
        return $result['status']==200?$this->serpath.$oss_name:"";
    }


    /**
     * @return string
     */
    private function create_sn(){
        mt_srand((double )microtime() * 1000000 );
        return date("YmdHis" ).str_pad( mt_rand( 1, 99999 ), 5, "0", STR_PAD_LEFT );
    }
}
